/**
 * @AUTHOR soft
 * @DATE 2018/12/26 11:36
 * @DESCRIBE
 */
var chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
var chnUnitChar = ["", "十", "百", "千"];
function numToChinese(val) {
  var num = val;
  var strIns = '', chnStr = '';
  var unitPos = 0;
  var zero = true;
  while (num > 0) {
    var v = num % 10;
    if (v === 0) {
      if (!zero) {
        zero = true;
        chnStr = chnNumChar[v] + chnStr;
      }
    } else {
      zero = false;
      strIns = chnNumChar[v];
      strIns += chnUnitChar[unitPos];
      chnStr = strIns + chnStr;
    }
    unitPos++;
    num = Math.floor(num / 10);
  }
  if (val === 0) chnStr = '零';
  return chnStr.replace(/^一十/, '十');
}

/**
 * 部门 职位 职位等级 联动
 * @param $ jquery对象
 * @param layer layer对象
 * @param form 表单
 * @param collage 学院Dom
 * @param collageId 默认的学院id
 * @param dept 部门Dom
 * @param duty 职位Dom
 * @param dutyLevel 职位等级Dom
 * @param root 根url
 * @param fun 回调
 */
function deptDutyLink($, layer, form, collage, collageId, dept, duty, dutyLevel, root, fun) {
  if (root == null) root = '';
// 职位切换监听
  form.on('select(dutyF)', function (source) {
    var level = $(source.elem).find('option[value=' + source.value + ']').data('level');
    setDutyLevel(level);
    form.render('select');
  });

  form.on('select(collageF)', function (source) {
    setDept(source.value);
  });

  setCollage(function (cog) {
    if (collageId == null) collageId = cog.id;
    setDept(collageId, function () {
      setDuty(fun);
    });
  });

  // 设置职位
  function setDuty(dutyBack) {
    post(root + '/duty/list', {}, function (data) {
      duty.html(''); // 清空下拉框
      var dutys = data.payload;
      for (var i = 0; i < dutys.length; i++) {
        var dt = dutys[i];
        var opt = '<option data-level="' + dt.maxLevel + '" value="' + dt.id + '">' + dt.name + '</option>';
        duty.append(opt);
        if (i === 0) setDutyLevel(dt.maxLevel);
      }
      dutyBack ? dutyBack() : null;
    });
  }

  // 设置部门
  function setDept(cgid, deptBack) {
    dept.html('');
    post(root + '/dept/list', {cgid: cgid}, function (data) {
      fullData(dept, data);
      deptBack ? deptBack() : null;
    });
  }

  // 设置学院
  function setCollage(collageBack) {
    post(root + '/collage/list', {}, function (data) {
      fullData(collage, data);
      var one = data.payload[0];
      collageBack ? collageBack(one) : null;
    });
  }

  // 填充数据
  function fullData(dom, data) {
    dom.html('');
    var items = data.payload;
    for (var i = 0; i < items.length; i++) {
      var item = items[i];
      var opt = '<option value="' + item.id + '">' + item.name + '</option>';
      dom.append(opt);
    }
  }
  // 通用的post请求
  function post(url, fd, successBack) {
    $.post(url, fd, function (data) {
      if (data.status === 200) {
        successBack ? successBack(data) : null;
      } else {
        layer.alert('网络异常！', {
          icon: 5
        }, function (idx) {
          layer.closeAll();
        });
      }
      form.render('select');
    });
  }

  // 设置职位等级下拉框
  function setDutyLevel(maxLevel) {
    dutyLevel.html('');
    for (var i = 1; i <= maxLevel; i++) {
      var val = numToChinese(i) + "级";
      var opt = '<option value="' + i + '">' + val + '</option>';
      dutyLevel.append(opt);
    }
  }
}

/**
 * 获取地位名
 * @param pos 地位数
 */
function getPos(pos) {
  if (pos === 1) {
    return "主持人";
  } else if (pos > 1) {
    return "参与人" + (pos - 1);
  } else {
    return "未知";
  }
}